package com.roger.springcloudGreenwich.listener;

import com.netflix.appinfo.InstanceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by admin on 2019/4/6.
 */
@Component
@Slf4j
public class EurekaStateChangeListener {


    @Value("${app.env}")
    private String appEnv;

    @EventListener
    public void listen(EurekaInstanceCanceledEvent event) {
        log.info("[{}],[{}],[{}] 服务下线",getCurrentTime(),event.getServerId(),event.getAppName());
        //System.err.println(event.getServerId() + "\t" + event.getAppName() + " 服务下线");
    }

    @EventListener
    public void listen(EurekaInstanceRegisteredEvent event) {
        if(event.isReplication()){
            InstanceInfo instanceInfo = event.getInstanceInfo();
            System.err.println(instanceInfo.getAppName() + "进行注册,发送邮件");

        }

    }

    @EventListener
    public void listen(EurekaInstanceRenewedEvent event) {
        //System.err.println(event.getServerId() + "\t" + event.getAppName() + " 服务进行续约");
    }

    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
        //System.err.println("注册中心 启动");
    }

    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        log.info("[{}] Eureka Server 启动",getCurrentTime());
    }


    private static String getCurrentTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }

}
